<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/metadata/productInfo/productInfoList' }">商品信息列表</el-breadcrumb-item>
      <el-breadcrumb-item>商品信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <div class="autumn-center">
      <div class="edit-form">
        <el-form size="large" label-width="90px" disabled :model="editForm">

          <el-row>
            <el-col :span="24">
              <el-form-item label="封面">
                <autumn-avatar v-model="editForm.coverImg" :preview="true"></autumn-avatar>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="分类" prop="category">
                <autumn-tree-select :data="treeCategorys" v-model="editForm.category" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="编码" prop="code">
                <el-input v-model="editForm.code" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="名称" prop="name">
                <el-input v-model="editForm.name" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="型号" prop="model">
                <el-input v-model="editForm.model" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="规格" prop="spec">
                <el-input v-model="editForm.spec" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单位" prop="unit">
                <el-select v-model="editForm.unit">
                  <el-option v-for="item in perUnits" :key="item.id" :label="item.code + '-' + item.name"
                    :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="售价" prop="unitPrice">
                <el-input-number v-model="editForm.unitPrice" :min="1" :max="99999999" :precision="2" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="图片">
                <autumn-upload ref="uploadRef" module="商品信息" :businessId="editForm.id" :preview="true"></autumn-upload>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="详情" prop="remark">
                <autumn-editor v-model:valueHtml="editForm.remark" :toolBar="false" :readOnly="true"></autumn-editor>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>

        <div class="form-foot-button">
          <el-button @click="cancel">返回</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { $convertTree, $listPerUnit } from 'src/util/tools'
import { viewProductInfo } from 'src/api/metadata/productInfo'
import { listProductCategory } from 'src/api/metadata/productCategory'


const route = useRoute()

// 查询商品分类
const treeCategorys = ref<Array<any>>([])
listProductCategory().then(({ data }) => {
  treeCategorys.value = $convertTree(data)
})
//查询计量单位
const perUnits = ref<Array<any>>([])
perUnits.value = $listPerUnit()


// 查看商品信息
const editForm = reactive({
  id: '',
  category: '',
  name: '',
  code: '',
  spec: '',
  model: '',
  coverImg: '',
  unitPrice: 0.01,
  remark: '',
  unit: ''
})
if (route.query.id) {
  const id = route.query.id as string
  viewProductInfo(id).then(({ data }) => {
    Object.assign(editForm, data)
  })
}

// 取消
const router = useRouter()
const cancel = () => {
  router.push('/metadata/productInfo/productInfoList')
}

</script>

<style lang="scss" scoped></style>
